from log import log
from util.sqlmapper import SQLManager

from entity.model_describe import ModelDescribe


class ModelDescribeDao:
    def setAll(self, m: ModelDescribe):
        with SQLManager() as db:
            return db.insert_return_key("insert into t_model_describe values (null,%s,%s,%s,%s,%s,%s)",
                                        m.model_layer_id,
                                        m.train_id,
                                        m.var1,
                                        m.var2,
                                        m.var3,
                                        m.var4)

    def findAllByTrainId(self, train_id: int):
        with SQLManager() as db:
            log.info("find a ModelDescribe in db")
            return db.get_all("select * from t_model_describe where train_id = %s", train_id)

    def deleteByTrainId(self, train_id: int):
        with SQLManager() as db:
            db.modify("delete from t_model_describe where train_id = %s", train_id)

    def findModelTypeByTrainId(self, train_id: int):
        with SQLManager() as db:

            msg = db.get_one(
                "select l.name,m.var1 from t_model_describe m inner join t_layers l  on m.model_layer_id = l.id  where train_id = %s limit 1",
                train_id)
            log.debug(msg)
            if msg == None:
                return msg
            if msg[0] == "ConvBase":
                return msg[1]
            else:
                return msg[0]

    def findById(self, id: int):
        with SQLManager() as db:
            list_line = db.get_one("select * from t_model_describe where id=%s", id)

            return list_line


if __name__ == "__main__":
    m = ModelDescribe()
    # m.train_id = 5
    # m.model_layer_id = 1
    # m.var1 = True
    # m.var2 = 3
    #
    # print(m.model_layer_id)
    md = ModelDescribeDao()
    # i = md.setExcludeImgName(m)
    # print(i)
    # print(md.findById(i))
    res = md.findAllByTrainId(14)
    for t in res:
        print(t[1:])

    print(md.findModelTypeByTrainId(11))
